import React, { Component } from 'react'

export default class Item extends Component {
  state={
    mosue:false
  }
  handleMouse = (type) =>{
    return () => {
      this.setState({mouse:type})
    }
  }
  handleCheck=(id) => { 
    return (event) => {
      this.props.updateItem(id,event.target.checked)
    }
  }
  delete = (id) => {
    const { deleteItem } = this.props
    return () => { 
      if(window.confirm('确定删除吗?')){
          deleteItem(id)
      }else{
        console.log('取消删除');
      }
     }
  }
  render() {
    const{id,name,check} = this.props
    const {mouse} = this.state
    return (
      <li style={{backgroundColor:mouse?'#ddd':'white'}} onMouseOver={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
        <label>
          <input type="checkbox" checked={check} onChange={this.handleCheck(id)}/>
          <span>{name}</span>
        </label>
        <button onClick={this.delete(id)} className="btn btn-danger" style={{display:mouse?'block':"none"}}>删除</button>
      </li>
    )
  }
}
